package com.example.wjy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.example.room.entity.PublicTeam;
import com.example.superyi.myteam.R;
import com.example.wjy.viewmodels.PublicTeamViewModel;

import java.util.List;

public class PublicTeamAdapter extends RecyclerView.Adapter<PublicTeamAdapter.PublicTeamViewHolder>{
    List<PublicTeam> teamList;

    public void setTeamList(List<PublicTeam> teamList) {
        this.teamList = teamList;
    }

    public static Context context;

    public void setContext(Context context) {
        this.context = context;
    }

    int []colors = {0x80FF4500,0x80FFB90F,0x8098F5FF,0x80836FFF,0x8066CD00,0x80B452CD};

    PublicTeamViewModel publicTeamViewModel;

    public void setPublicTeamViewModel(PublicTeamViewModel publicTeamViewModel) {
        this.publicTeamViewModel = publicTeamViewModel;
    }

    public PublicTeamAdapter(List<PublicTeam> teams) {

        this.teamList = teams;
    }

    @NonNull
    @Override
    public PublicTeamAdapter.PublicTeamViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.public_item_layout, parent, false);
        return new PublicTeamAdapter.PublicTeamViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PublicTeamAdapter.PublicTeamViewHolder holder, int position) {
        PublicTeam team = teamList.get(position);
        holder.teamName.setText(team.getTeamName());
        holder.teamKind.setText(team.getTeamKind());
        holder.teamIntro.setText(team.getTeamIntro());
        holder.teamNumOfAll.setText(String.valueOf(team.getTeamNumOfAll()));
        holder.cv.setBackgroundColor(colors[position % 6]);
    }

    @Override
    public int getItemCount() {
        return teamList == null ? 0 : teamList.size();
    }

    static class PublicTeamViewHolder extends RecyclerView.ViewHolder{
        TextView teamName;

        TextView teamKind;

        TextView teamNumOfAll;

        TextView teamIntro;

        TextView teamId;

        Button enter;
        CardView cv;
        public PublicTeamViewHolder(@NonNull View itemView) {
            super(itemView);
            teamName = itemView.findViewById(R.id.team_name);
            teamKind = itemView.findViewById(R.id.team_kind);
            teamIntro = itemView.findViewById(R.id.team_intro);
            teamNumOfAll = itemView.findViewById(R.id.team_num_of_all);
            teamId = itemView.findViewById(R.id.team_id);
            enter = itemView.findViewById(R.id.enter);
            enter.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "申请加入", Toast.LENGTH_SHORT).show();
                }
            });
            cv = itemView.findViewById(R.id.team_item);
        }
    }
}
